package com.xinzhidi.web.equipment.controller;

import com.xinzhidi.common.annotation.Log;
import com.xinzhidi.common.core.controller.BaseController;
import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.core.domain.R;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.enums.BusinessType;
import com.xinzhidi.common.utils.poi.ExcelUtil;
import com.xinzhidi.web.equipment.domain.dto.EquipmentGroupAddDto;
import com.xinzhidi.web.equipment.domain.dto.EquipmentGroupListDto;
import com.xinzhidi.web.equipment.domain.dto.EquipmentGroupUpdDto;
import com.xinzhidi.web.equipment.domain.vo.EquipmentGroupInfoVo;
import com.xinzhidi.web.equipment.domain.vo.EquipmentGroupListVo;
import com.xinzhidi.web.equipment.service.IEquipmentGroupService;
import com.xinzhidi.web.plot.domain.vo.SelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 设备分组Controller
 *
 * @author ruoyi
 * @date 2023-05-26
 */
@RestController
@Api(tags = "设备分组")
@RequestMapping("/equipment")
public class EquipmentGroupController extends BaseController {
    @Autowired
    private IEquipmentGroupService equipmentGroupService;

    @ApiOperation("查询设备分组列表")
    @PreAuthorize("@ss.hasPermi('equipment:equipment:list')")
    @PostMapping("/listEquipmentGroup")
    public TableDataInfo<EquipmentGroupListVo> listEquipmentGroup(@Validated @RequestBody EquipmentGroupListDto dto) {
        startPagePost(dto);
        return equipmentGroupService.selectEquipmentGroupList(dto);
    }

    @ApiOperation("查询设备分组下拉列表")
    @PostMapping("/listEquipmentGroupSelect")
    public TableDataInfo<SelectVo> listEquipmentGroupSelect(@Validated @RequestBody EquipmentGroupListDto dto) {
        dto.setPageSize(9999);
        startPagePost(dto);
        return equipmentGroupService.listEquipmentGroupSelect(dto);
    }

    @ApiOperation("导出设备分组列表")
    @PreAuthorize("@ss.hasPermi('equipment:equipment:export')")
    @Log(title = "设备分组", businessType = BusinessType.EXPORT)
    @PostMapping("/exportEquipmentGroup")
    public void exportEquipmentGroup(HttpServletResponse response, EquipmentGroupListDto dto) {
        TableDataInfo<EquipmentGroupListVo> list = equipmentGroupService.selectEquipmentGroupList(dto);
        ExcelUtil<EquipmentGroupListVo> util = new ExcelUtil<EquipmentGroupListVo>(EquipmentGroupListVo.class);
        util.exportExcel(response, list.getRows(), "设备分组数据");
    }

    @ApiOperation("获取设备分组详细信息")
    @PreAuthorize("@ss.hasPermi('equipment:equipment:query')")
    @GetMapping(value = "/getEquipmentGroupById")
    public R<EquipmentGroupInfoVo> getEquipmentGroupById(@RequestParam("id") Long id) {
        return R.ok(equipmentGroupService.selectEquipmentGroupById(id));
    }

    @ApiOperation("新增设备分组")
    @PreAuthorize("@ss.hasPermi('equipment:equipment:add')")
    @Log(title = "设备分组", businessType = BusinessType.INSERT)
    @PostMapping("addEquipmentGroup")
    public AjaxResult addEquipmentGroup(@Validated @RequestBody EquipmentGroupAddDto dto) {
        return toAjax(equipmentGroupService.insertEquipmentGroup(dto));
    }

    @ApiOperation("修改设备分组")
    @PreAuthorize("@ss.hasPermi('equipment:equipment:edit')")
    @Log(title = "设备分组", businessType = BusinessType.UPDATE)
    @PostMapping("editEquipmentGroup")
    public AjaxResult editEquipmentGroup(@Validated @RequestBody EquipmentGroupUpdDto dto) {
        return toAjax(equipmentGroupService.updateEquipmentGroup(dto));
    }

    @ApiOperation("删除设备分组")
    @PreAuthorize("@ss.hasPermi('equipment:equipment:remove')")
    @Log(title = "设备分组", businessType = BusinessType.DELETE)
    @DeleteMapping("/delEquipmentGroup")
    public AjaxResult delEquipmentGroup(@RequestParam Long[] ids) {
        return toAjax(equipmentGroupService.deleteEquipmentGroupByIds(ids));
    }
}
